package org.oschina.api.upload.dao;

import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.upload.model.UploadConfig;
import org.oschina.api.upload.vo.UploadConfigVo;
import org.springframework.stereotype.Repository;

@Repository
public class UploadConfigDAO extends BaseDAO {

	private final String findVo[]={"id","name","path","size","type"};
	
	/**
	 * 返回
	 * @return
	 */
	public List<UploadConfigVo> findVo()
	{
		return getQueryHelper().query_cache(findVo, null, null, null, UploadConfigVo.class, -1, -1);
	}

	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public UploadConfigVo findVoById(int id)
	{
		UploadConfigVo uploadConfigVo=(UploadConfigVo) getCacheHelper().hget(getKey(Constants.CACHE_UPLOAD_CONFIG, id), UploadConfigVo.class);
		if(uploadConfigVo!=null)
			return uploadConfigVo;
		
		return _findVoById(id);
	}
	private UploadConfigVo _findVoById(int id)
	{
		return (UploadConfigVo) getQueryHelper().queryForObject_cache(findVo, new String[]{"id=?"}, new Object[]{id}, UploadConfigVo.class);
	}
	
	/**
	 * 更新
	 * @param uploadConfig
	 * @return
	 */
	public int update(UploadConfig uploadConfig)
	{
		return (int) getQueryHelper().update_cache(new String[]{"name=?","path=?","size=?","type=?"}, new String[]{"id=?"}, 
				new Object[]{uploadConfig.getName(),uploadConfig.getPath(),uploadConfig.getSize(),uploadConfig.getType(),uploadConfig.getId()}, UploadConfigVo.class);
	}
}